﻿@Cofe ?= {}

$ = jQuery

Entry = Cofe.Entry
EntryList = Cofe.EntryList
EntryListView = Cofe.EntryListView
EntryTreeView = Cofe.EntryTreeView
EntryMetaView = Cofe.EntryMetaView
EntrySearchView = Cofe.EntrySearchView
EntryUploadView = Cofe.EntryUploadView
EventView = Cofe.EventView
utils = Cofe.utils

class App extends Spine.Controller
  elements : 
    "#treeView" : "eleTreeView"
    "#listView" : "eleListView"
    "#metaView" : "eleMetaView"
    "#searchView" : "eleSearchView"
    "#uploadView" : "eleUploadView"
    "#eventView" : "eleEventView"
  
  currentRootEntry : null

  changeSearch : (sender, newSearchStr) =>
    newSearchStr = 'root:\"' + @currentRootEntry.path + '\" ' + newSearchStr
    url = utils.formatSearch(newSearchStr, null, 20, 1)
    EntryList.remove(url)
    @listView.changeUrlPath(url)

  changePath : (sender, entryItem) =>
    if (entryItem isnt undefined)
      @currentRootEntry = entryItem
      @searchView.notifyPathChanged(entryItem.path)
      @listView.changeUrlPath(utils.formatPath(entryItem.path, { rel : "list" }, 20, 1), entryItem)
      if sender isnt @treeView
        @treeView.select(entryItem.path)
      @metaView.changeEntries([entryItem])
      #@uploadView.changeRootEntry(entryItem)
  
  updateMeta : (sender, selectedItems) =>
    @metaView.changeEntries(selectedItems)

  error : (sender, errorStr, url) ->
    console.log(errorStr)
    utils.notify(url, errorStr)

  init : ->
    @listView = new EntryListView({ el : @eleListView, waypoint : true, select : 'multi' })
    @treeView = new EntryTreeView({ el : @eleTreeView })
    @metaView = new EntryMetaView({ el : @eleMetaView })
    @searchView = new EntrySearchView({ el : @eleSearchView })
    @eventView = new EventView({ el : @eleEventView })
    #@uploadView = new EntryUploadView({ el : @eleUploadView })

    @treeView.bind("selectionChanged", @changePath)
    @listView.bind("itemClicked", @changePath)
    @listView.bind("selectionChanged", @updateMeta)
    @treeView.bind("error", @error)
    @listView.bind("error", @error)
    @searchView.bind("searchChanged", @changeSearch)
    @searchView.bind("searchCancelled", () => @changePath(@, @currentRootEntry))
    @eventView.bind("event", (s, ei) -> utils.notify(ei.event.title, ei.event.description))

    rootList = EntryList.fromUrl(utils.formatPath("", {rel : "list" }))
    rootList.bind("fetched", () =>
      if rootList.entries.length > 0
        for e in rootList.entries
          @treeView.addRootEntry(e)
        @changePath(@, @treeView.rootEntryItems[0].entry)
    )
    rootList.fetch()


#    $('#app > div:first').resizable({
#      handles: 'e',
#      minWidth : '100',
#      maxWidth : '400',
#      resize: () ->
#         remainingSpace = $(@).parent().width() - $(@).outerWidth()
#         divTwo = $(@).next()
#         divTwoWidth = remainingSpace - (divTwo.outerWidth() - divTwo.width())
#         divTwo.css('width', divTwoWidth + 'px')
#    });
  
  render : ->
    #@listView.render()
    #@treeView.render()
  
Cofe.App = App